/*
 * lector.cpp
 *
 *  Created on: Oct 30, 2013
 *      Author: m
 */

#include <iostream>
#include <cstdlib>
#include <ctime>
#include "DefinicionesComunes.h"
#include "NewMessageQueue.h"
#include "NewSemaphore.h"
#include "NewSharedMemory.h"
#include "Sequence.h"
#include "CPrinter.h"

using namespace ipc;

int main ( int argc, char* argv[] )
{
    int nLectores;
    float valorDolar;
    int id;

    srand( time( NULL ) );

    id = Sequence::get( IPC_BASE_FILE, SEQ_ID_LECTOR ).getNext();

    CPrinter::setHeader( "lector", id );

    // IPCs
    SharedMemory<float> shmPrecioDolar =
            SharedMemory<float>::get( IPC_BASE_FILE, SHM_BOARD );
    SharedMemory<int> shmContadorLectores =
            SharedMemory<int>::get( IPC_BASE_FILE, SHM_CONT_LECT );
    Semaphore semEscritor =
            Semaphore::get( IPC_BASE_FILE, SEM_ESCRITOR );
    Semaphore mutexContadorLectores =
            Semaphore::get( IPC_BASE_FILE, SEM_CONT_LECT );

    CPrinter::printOut( "INICIADO" );

    Semaphore::get( IPC_BASE_FILE, SEM_INICIO ).p();

    while ( true )
    {
        sleep( random() % 10 );

        mutexContadorLectores.p();

        nLectores = shmContadorLectores.read();
        nLectores++;
        shmContadorLectores.write( nLectores );

        if ( nLectores == 1 )
        {
            semEscritor.p();
        }

        mutexContadorLectores.v();

        sleep( random() % 10 );

        valorDolar = shmPrecioDolar.read();

        CPrinter::printOut( "Valor del dolar: $%.2f", valorDolar );

        mutexContadorLectores.p();

        nLectores = shmContadorLectores.read();
        nLectores--;
        shmContadorLectores.write( nLectores );

        mutexContadorLectores.v();

        if ( nLectores == 0 )
        {
            CPrinter::printOut( "YA NO QUEDAN LECTORES!" );

            semEscritor.v();
        }
    }

    return 0;
}
